package com.shanzmoo.merchant.server.api.aspect;

import com.shanzmoo.base.constant.e.LoginUserType;
import com.shanzmoo.merchant.server.api.domain.AuthUser;
import com.shanzmoo.merchant.server.api.util.SecurityUtil;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.stereotype.Component;

/**
 * 用户权限拦截
 *
 * @author fs
 */
@Slf4j
@Lazy
@Aspect
@Component
@AllArgsConstructor
@Order(1)
public class UserAuthAspect {
    @SneakyThrows
    @Before("@annotation(userAuth)")
    public void before(JoinPoint point, UserAuth userAuth) {
        AuthUser user = SecurityUtil.getCurrentUser();
        boolean flag = false;
        if(user != null){
            for(LoginUserType userType:userAuth.value()){
                if(userType == user.getUserType()){
                    flag = true;
                    break;
                }
            }
        }
        if (!flag) {
            throw new AccessDeniedException("权限不足");
        }
    }
}
